
package restaurante.cih;


import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import restaurante.cci.CtrlInterno;
import restaurante.cdp.Funcionario;
import restaurante.cdp.enuns.TipoFuncionario;
import restaurante.util.Rest.tipo;

public class JanFormularioFuncionario extends javax.swing.JFrame {

    private static JanFormularioFuncionario instance;
    private CtrlInterno c;
    List<Funcionario> listFuncionario = new ArrayList<>();
    private static JanEventosFuncionario janEventosFuncionario;
    private tipo MODO;
    private Funcionario funcionario;
    

    public tipo getMODO() {
        return MODO;
    }

    public void setMODO(tipo MODO) {
        this.MODO = MODO;
    }

    public List<Funcionario> getListFuncionario() {
        return listFuncionario;
    }

    public void setListFuncionario(List<Funcionario> listFuncionario) {
        this.listFuncionario = listFuncionario;
    }
  
    
    
    /**
     * Obtém uma instância da aplicação. Implementação do padrão Singleton.
     */
    public static JanFormularioFuncionario getInstance() throws Exception{
        if(instance == null){
            instance = new JanFormularioFuncionario();
            janEventosFuncionario = JanEventosFuncionario.getInstance(); 
            
        }
        return instance;
    }
    
    /**
     * Creates new form JanFormularioEmpresas
     */
    private JanFormularioFuncionario() throws Exception {
        initComponents();
        this.setLocationRelativeTo(null);
        List<TipoFuncionario> listFunc = new ArrayList<>();
        listFunc.add(TipoFuncionario.CAIXA);
        listFunc.add(TipoFuncionario.COZINHEIRO);
        listFunc.add(TipoFuncionario.GARÇOM);
        listFunc.add(TipoFuncionario.MOTOBOY);
        listFunc.add(TipoFuncionario.TELEFONISTA);
        jComboBox1.setModel(new DefaultComboBoxModel(listFunc.toArray()));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel12 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jLabel3.setText("jLabel3");

        setTitle("Cadastro");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Funcionário");
        getContentPane().add(jLabel1, java.awt.BorderLayout.PAGE_START);

        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jPanel3.setLayout(new java.awt.GridLayout(4, 2));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Nome:");
        jPanel3.add(jLabel6);

        jTextField1.setMinimumSize(new java.awt.Dimension(90, 25));
        jTextField1.setName(""); // NOI18N
        jTextField1.setPreferredSize(new java.awt.Dimension(90, 25));
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jPanel3.add(jTextField1);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("CPF:");
        jPanel3.add(jLabel7);

        jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jPanel3.add(jFormattedTextField1);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Salário:");
        jPanel3.add(jLabel9);
        jPanel3.add(jTextField2);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel10.setText("Tipo:");
        jPanel3.add(jLabel10);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setMinimumSize(new java.awt.Dimension(130, 25));
        jComboBox1.setPreferredSize(new java.awt.Dimension(130, 25));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 8, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel3.add(jPanel4);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(33, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jButton1.setText("Salvar");
        jButton1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel12.add(jButton1);

        jButton2.setText("Voltar");
        jButton2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jButton2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel12.add(jButton2);

        getContentPane().add(jPanel12, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        List componentes = new ArrayList();
        componentes.add(this.jTextField1.getText());
        componentes.add(this.jFormattedTextField1.getText());
        componentes.add(this.jTextField2.getText());
        componentes.add(this.jComboBox1.getSelectedItem());          
        acaoSalvar(componentes);               
            
            
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        acaoVoltar();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {  
            c = CtrlInterno.getInstance();
        } catch (Exception ex) {
            Logger.getLogger(JanFormularioProduto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formWindowOpened

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
       acaoVoltar();
    }//GEN-LAST:event_formWindowClosed

    private void formWindowDeactivated(java.awt.event.WindowEvent evt) {
        acaoVoltar();
    }
     
    
     
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton jButton1;
    public javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

    
    public void acaoIncluir() {
        arrumarTela(true);
        jButton1.setVisible(true);
        jComboBox1.setEnabled(true);
        setVisible(true);
    }

    public void arrumarTela(boolean b) {

        jTextField1.setEnabled(b);
        jFormattedTextField1.setEnabled(b);
        jTextField2.setEnabled(b);
        jComboBox1.setEnabled(b);


    }

    public void acaoSalvar(List componentes) {
        String msg = validaCampos();
        if(msg.equals(""))
        {
            if(validarTextosFormatados())
            {
                if (MODO != tipo.INCLUIR) {
                    jComboBox1.setEnabled(false);
                    c.alterarFuncionario(componentes, funcionario);
                } 
                else {

                    limparTela();

                    funcionario = c.incluirFuncionario(componentes);
                }
                setVisible(false);

                c.dadosFuncionario(janEventosFuncionario.jTable1.getModel(), listFuncionario.size());
            }
        }
        else
        {
            System.out.println("Informe:\n\n"+msg);
        }
    }

    public void limparTela() {
        jTextField1.setText("");
        jFormattedTextField1.setText("");
        jTextField2.setText("");
    }

    public void acaoVoltar() {
        limparTela();
        setVisible(false);
    }


    public void acaoAlterar(int linhaSelecionada) {
        Funcionario f = listFuncionario.get(linhaSelecionada);
        montarTela(f);
        funcionario = f;
        arrumarTela(true);
        jButton1.setVisible(true);
        jComboBox1.setEnabled(false);
        setVisible(true);
    }

    public void montarTela(Funcionario f) {
        jTextField1.setText(f.getNome());
        jFormattedTextField1.setText(f.getCpf().toString());
        jTextField2.setText(f.getSalario().toString());
        jComboBox1.setSelectedItem(f.getTipoFuncionario());
    }

    public void acaoConsultar(int selectedRow) {
        funcionario = listFuncionario.get(selectedRow);
        montarTela(funcionario);
        arrumarTela(false);
        jButton1.setVisible(false);
        setVisible(true);
    }
    
    public String validaCampos()
    {
        String msg = "";
        if(jTextField1.getText().equals(""))
        {
            msg+= "-Nome\n";
        }
        if(jFormattedTextField1.getText().equals(""))
        {
            msg+= "-CPF\n";
        }
        if(jTextField2.getText().equals(""))
        {
            msg+= "-Salário\n";
        }
        return msg;
    }
    
    public boolean validarTextosFormatados()
    {
        if(((Long.valueOf((String) jFormattedTextField1.getText()))<0))
        {
            JOptionPane.showMessageDialog(this, "CPF Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        else if((Double.valueOf((String) jTextField2.getText()))<0)
        {
            JOptionPane.showMessageDialog(this, "Sálario Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        return true;
    }
}

